package com.logisticsrepair.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class Public {
    public static final String SYS_IMG_PATH = "src/main/resources/static/uploadImg";

    /**
     * 获取用户图像绝对路径
     *
     * @param mainPath 主目录
     * @param subPath  子目录
     * @return 用户图片File对象
     */
    public static File getUserPhotoFile(String mainPath, String subPath) {
        StringBuffer path = new StringBuffer();
        path.append(new File(SYS_IMG_PATH).getAbsolutePath()).append("/").append(mainPath).append("/").append(subPath);
        return new File(path.toString());
    }

    /**
     * 保存用户图像
     *
     * @param file     用户上传文件
     * @param mainPath 主目录
     * @param subPath  子目录
     * @return File对象
     * @throws IOException
     */
    public static File saveUserPhoto(MultipartFile file, String mainPath, String subPath) throws IOException {
        File userPhotoFile = getUserPhotoFile(mainPath, subPath);
 
        // 添加详细调试日志
        System.out.println("========== 图片上传调试信息 ==========");
        System.out.println("原始文件名: " + file.getOriginalFilename());
        System.out.println("文件大小: " + file.getSize() + " bytes");
        System.out.println("相对路径: " + SYS_IMG_PATH);
        System.out.println("当前工作目录: " + System.getProperty("user.dir"));
        System.out.println("目标目录: " + userPhotoFile.getAbsolutePath());
        
        if (!userPhotoFile.exists()) {
            boolean created = userPhotoFile.mkdirs();
            System.out.println("创建目录结果: " + created);
        } else {
            System.out.println("目录已存在");
        }
        
        File saveFile = new File(userPhotoFile.getAbsolutePath() + "/" + file.getOriginalFilename());
        System.out.println("完整保存路径: " + saveFile.getAbsolutePath());
        
        file.transferTo(saveFile);
        System.out.println("文件保存成功！");
        System.out.println("文件是否存在: " + saveFile.exists());
        System.out.println("====================================");
        
        return saveFile;
    }

    /**
     * 删除用户图像（递归删除）
     *
     * @param openId  用户openId
     * @param orderId 用户orderId
     */
    public static void deleteUserPhoto(String openId, String orderId) {
        File userPhotoFile = getUserPhotoFile(openId, orderId);
        if (userPhotoFile.exists()) {
            Path directory = Paths.get(userPhotoFile.getAbsolutePath());
            try {
                Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException {
                        Files.delete(file); // 有效，因为它始终是一个文件
                        return FileVisitResult.CONTINUE;
                    }

                    @Override
                    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                        Files.delete(dir); //这将起作用，因为目录中的文件已被删除
                        return FileVisitResult.CONTINUE;
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
